package com.cms.web.servlet;

import com.cms.pojo.Admin;
import com.cms.pojo.Course;
import com.cms.pojo.Sc;
import com.cms.service.CourseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectCourseServlet")
public class SelectCourseServlet extends HttpServlet {
    private CourseService courseService = new CourseService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 课程id
        int id = Integer.parseInt(request.getParameter("id"));

        Course course = courseService.selectById(id);

        if (course.getSelected() == course.getTotalnum()) {
            // 课选满了，不可选
            request.getRequestDispatcher("/noSelectCourseServlet").forward(request, response);
        } else {
            // 课未选满，可选
            HttpSession session = request.getSession();

            Admin user = (Admin) session.getAttribute("user");

            Sc sc = new Sc();
            sc.setC_id(id);
            sc.setS_id(user.getId());

            // 调用CourseService，添加sc表内容，增加课程已选课人数
            courseService.selectCourse(sc);
            courseService.addSelected(id);

            request.getRequestDispatcher("/noSelectCourseServlet").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
